package com.phiture.erp.basic.web.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import com.phiture.erp.basic.core.pojo.dto.ErpMaterialPlanPageReqDTO;
import com.phiture.erp.basic.core.pojo.dto.ErpMaterialPlanSaveReqDTO;
import com.phiture.erp.basic.core.pojo.vo.ErpMaterialPlanRespVO;
import com.phiture.erp.basic.core.service.ErpMaterialPlanService;
import com.phiture.erp.basic.dal.entity.ErpMaterialPlanDO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 物料计划资料")
@RestController
@RequestMapping("/erp/material-plan")
public class ErpMaterialPlanController {

    @Resource
    private ErpMaterialPlanService materialPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建物料计划资料")
    @PreAuthorize("@ss.hasPermission('erp:material-plan:create')")
    public CommonResult<Long> createMaterialPlan(@Valid @RequestBody ErpMaterialPlanSaveReqDTO createReqDTO) {
        return success(materialPlanService.createMaterialPlan(createReqDTO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料计划资料")
    @PreAuthorize("@ss.hasPermission('erp:material-plan:update')")
    public CommonResult<Boolean> updateMaterialPlan(@Valid @RequestBody ErpMaterialPlanSaveReqDTO updateReqDTO) {
        materialPlanService.updateMaterialPlan(updateReqDTO);
        return success(true);
    }

    @PutMapping("/createOrUpdate")
    @Operation(summary = "创建或者更新物料计划资料")
    @PreAuthorize("@ss.hasPermission('erp:material-plan:update')")
    public CommonResult<Boolean> createOrUpdateMaterialPlan(@Valid @RequestBody ErpMaterialPlanSaveReqDTO reqDTO) {
        materialPlanService.createOrUpdateMaterialPlan(reqDTO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料计划资料")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:material-plan:delete')")
    public CommonResult<Boolean> deleteMaterialPlan(@RequestParam("id") Long id) {
        materialPlanService.deleteMaterialPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料计划资料")
    @Parameter(name = "materialId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:material-plan:query')")
    public CommonResult<ErpMaterialPlanRespVO> getMaterialPlan(@RequestParam("materialId") Long materialId) {
        ErpMaterialPlanDO materialPlan = materialPlanService.getMaterialPlanByMaterialId(materialId);
        return success(BeanUtils.toBean(materialPlan, ErpMaterialPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料计划资料分页")
    @PreAuthorize("@ss.hasPermission('erp:material-plan:query')")
    public CommonResult<PageResult<ErpMaterialPlanRespVO>> getMaterialPlanPage(@Valid ErpMaterialPlanPageReqDTO pageReqDTO) {
        PageResult<ErpMaterialPlanDO> pageResult = materialPlanService.getMaterialPlanPage(pageReqDTO);
        return success(BeanUtils.toBean(pageResult, ErpMaterialPlanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料计划资料 Excel")
    @PreAuthorize("@ss.hasPermission('erp:material-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialPlanExcel(@Valid ErpMaterialPlanPageReqDTO pageReqDTO, HttpServletResponse response) throws IOException {
        pageReqDTO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpMaterialPlanDO> list = materialPlanService.getMaterialPlanPage(pageReqDTO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料计划资料.xls", "数据", ErpMaterialPlanRespVO.class,
                        BeanUtils.toBean(list, ErpMaterialPlanRespVO.class));
    }

}